﻿[
{
    "name": "Open file",
    "desc": "Controls device settings on the microphone.",
    "source": function () {
        var fileOpenPicker = new Windows.Storage.Pickers.FileOpenPicker();
        fileOpenPicker.fileTypeFilter.append(".txt");
        fileOpenPicker.commitButtonText = "Open a txt file";
        fileOpenPicker.pickSingleFileAsync().then(function (file) {
            if (!file) {
                console.error("No file opened");
            }
            console.log("File ", file.name, {
                "File Path": file.path,
                "Created Date": file.dateCreated,
                "Modified Date": file.dateModified
            });
        });
    }
},
{
    "name": "Save file",
    "desc": "Save the user image to a user specified location",
    "source": function () {
        var fileSavePicker = new Windows.Storage.Pickers.FileSavePicker();
        fileSavePicker.fileTypeChoices.insert("Image", [".png", ".jpg", ".jpeg"]);
        fileSavePicker.pickSaveFileAsync().then(function (file) {
            var userImage = Windows.System.UserProfile.UserInformation.getUserImage(Windows.System.UserProfile.UserImageKind.smallBitmap);
            userImage.copyAndReplaceAsync(file).then(function () {
                console.log("User image written");
            });
        }, function (e) {
            console.error(e.message);
        });
    }
},
{
    "name": "Read File",
    "desc": "Select a file and read the contents of that text file",
    "source": function () {
        var fileOpenPicker = new Windows.Storage.Pickers.FileOpenPicker();
        fileOpenPicker.fileTypeFilter.append(".txt");
        fileOpenPicker.pickSingleFileAsync().then(function (file) {
            file.openAsync(Windows.Storage.FileAccessMode.readWriteUnsafe).then(function (stream) {
                var dataReader = new Windows.Storage.Streams.DataReader(stream.getInputStreamAt(0));
                var data = [];
                function read(num) {
                    dataReader.loadAsync(num).then(function (count) {
                        data.push(dataReader.readString(count));
                        if (count === num) {
                            read(num);
                        }
                        else {
                            console.log("Data Read ", data);
                        }
                    });
                }
                console.log("Reading 5 bytes at a time");
                read(5);
            }, function () {
                console.log("Error")
            });
        }, function (e) {
            console.error(e.message);
        });
    }
},
{
    "name": "Write File",
    "desc": "Select a file and write the contents to text file",
    "source": function () {
        var fileSavePicker = new Windows.Storage.Pickers.FileSavePicker();
        fileSavePicker.fileTypeChoices.insert("Text", [".txt"]);
        fileSavePicker.pickSaveFileAsync().then(function (file) {
            file.openAsync(Windows.Storage.FileAccessMode.readWriteUnsafe).then(function (stream) {
                var dataWriter = new Windows.Storage.Streams.DataWriter(stream.getOutputStreamAt(0));
                dataWriter.writeString("Hello World " + new Date());
                dataWriter.storeAsync().then(function () {
                    console.log("Contents written to file");
                });
            }, function (e) {
                console.error("Error", e.message);
            });
        }, function (e) {
            console.error("Error", e.message);
        });
    }
},
{
    "name": "Delete a file",
    "desc": "Deletes a file that the user selects",
    "source": function () {
        var fileOpenPicker = new Windows.Storage.Pickers.FileOpenPicker();
        fileOpenPicker.fileTypeFilter.append(".txt");
        fileOpenPicker.pickSingleFileAsync().then(function (file) {
            file.deleteAsync(Windows.Storage.StorageDeleteOption.locationBasedRecycleBinBehavior).then(function (e) {
                console.log("File successfully deleted", file.path);
            }, function () {
                console.error("File could not deleted", file.path);
            });
        });
    }
},
{
    "name": "Rename a file",
    "desc": "Renames a file that the user selects",
    "source": function () {
        var fileOpenPicker = new Windows.Storage.Pickers.FileOpenPicker();
        fileOpenPicker.fileTypeFilter.append(".txt");
        fileOpenPicker.pickSingleFileAsync().then(function (file) {
            file.renameAsync("newName.txt", Windows.Storage.NameCollisionOption.generateUniqueName).then(function (e) {
                console.log("File successfully renamed", file.path);
            }, function () {
                console.error("File could not renamed", file.path);
            });
        });
    }
},

]